package cn.fl.fgi.service.impl;

import cmm.comm.facade.framework.api.ISysDictionaryApiService;
import cmm.mid.core.framework.page.KeyValueVO;
import cmm.mid.core.framework.page.Pager;
import cmm.user.facade.system.api.ISysOrgApiService;
import cmm.user.facade.system.dto.SysOrgDTO;
import cn.cmm.base.framework.constant.BaseExceptionConstant;
import cn.cmm.base.framework.exception.BaseException;
import cn.cmm.base.framework.logger.eum.LogOperEnum;
import cn.cmm.base.framework.utils.BaseLogUtils;
import cn.cmm.base.framework.utils.CollectionUtils;
import cn.cmm.base.framework.utils.EncryptUtil;
import cn.cmm.base.framework.utils.StringUtils;
import cn.fl.commons.result.ResponseData;
import cn.fl.fgi.api.IFgiBusInfoApiService;
import cn.fl.fgi.api.IFgiConInfoApiService;
import cn.fl.fgi.api.IFgiUserInfoApiService;
import cn.fl.fgi.api.condition.FgiBusInfoQueryCondition;
import cn.fl.fgi.api.condition.FgiUserInfoQueryCondition;
import cn.fl.fgi.api.dto.FgiBusInfoDTO;
import cn.fl.fgi.api.dto.FgiConInfoDTO;
import cn.fl.fgi.api.dto.FgiUserInfoWebDTO;
import cn.fl.fgi.service.IFgiBusInfoservice;
import cn.fl.fgi.transform.FgiBusInfoVOTransform;
import cn.fl.fgi.vo.FgiBusInfoVO;
import cn.fl.framework.base.context.CurrentThreadContext;
import cn.fl.framework.constant.DictionaryConstant;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import fl.constant.facade.framework.constant.BusinessIdConstant;
import fl.spl.facade.framework.constant.SplSystemConstant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @description 业务信息
 * @author: fengzuoxiang
 * @date: 19:56 2020/4/10
 * @version V1.0
 */
@Service
public class FgiBusInfoServiceImpl implements IFgiBusInfoservice {

    @Autowired
    private IFgiBusInfoApiService fgiBusInfoApiService;

    /**
     * 字典服务
     */
    @Autowired
    private ISysDictionaryApiService sysDictionaryApiService;

    /**
     * 组织架构API
     */
    @Autowired
    private ISysOrgApiService sysOrgApiService;

    @Autowired
    private IFgiUserInfoApiService fgiUserInfoApiService;

    @Autowired
    private IFgiConInfoApiService fgiConInfoApiService;

    @Override
    public Pager searchListPage(Pager page) throws BaseException {
        if (null == page || null == page.getCondition()) {
            return page;
        }
        try {
            FgiBusInfoQueryCondition fgiBusInfoQueryCondition =(FgiBusInfoQueryCondition) page.getCondition();
            String resultStr = this.fgiBusInfoApiService.selectListPage(fgiBusInfoQueryCondition);
            JSONObject jsonObject = JSON.parseObject(resultStr);
            List<FgiBusInfoVO> list = JSON.parseArray(jsonObject.get("records").toString(), FgiBusInfoVO.class);
            page.setResultList(list);
            page.setTotalCount(jsonObject.getInteger("total"));
        } catch (Exception e) {
            BaseLogUtils.newWebLogger("查询异常").setLogOperType(LogOperEnum.RESP)
                    .setSysCd(CurrentThreadContext.getCurrentSysCd())
                    .setChannelCd(SplSystemConstant.FL_WEB_CHL_CD)
                    .setBussinessId(BusinessIdConstant.LOG_SPL_MANAGER_CD)
                    .setWarning(false)
                    .setThrowable(e)
                    .error(BaseExceptionConstant.COMMON_0002_NAME);
        }

        return page;
    }


    @Override
    public ResponseData submitFgiBusInfo(FgiBusInfoQueryCondition fgiBusInfoQueryCondition) throws BaseException {
        if (null == fgiBusInfoQueryCondition || StringUtils.isEmpty(fgiBusInfoQueryCondition.getIsRisk())) {
            return ResponseData.error();
        }
        return this.fgiBusInfoApiService.submitFgiBusInfo(fgiBusInfoQueryCondition);
    }

    @Override
    public ResponseData makeSureSubmitFgiBusInfo(FgiBusInfoQueryCondition fgiBusInfoQueryCondition) throws BaseException {
        if (null == fgiBusInfoQueryCondition || StringUtils.isEmpty(fgiBusInfoQueryCondition.getIsSub())) {
            return ResponseData.error();
        }
        return this.fgiBusInfoApiService.makeSureSubmitFgiBusInfo(fgiBusInfoQueryCondition);
    }

    public List<KeyValueVO> getKvPrjTypeList() throws BaseException {
        return this.sysDictionaryApiService.getKvListAll(DictionaryConstant.CODE_PRJ_TYPE_CD, SplSystemConstant.FL_SYS_CD, SplSystemConstant.DEFAULT_ORG_CD);
    }

    @Override
    public List<KeyValueVO> getKvOrgList() throws BaseException {
        List<SysOrgDTO> list = this.sysOrgApiService.searchListByOrgTypCd(CurrentThreadContext.getFlServiceSysCd());
        List<KeyValueVO> kvList = new ArrayList<KeyValueVO>();
        if (CollectionUtils.isEmpty(list)) {
            return kvList;
        }
        for (SysOrgDTO sysOrgDTO : list) {
            KeyValueVO kv = new KeyValueVO();
            kv.setKey(sysOrgDTO.getOrgCd());
            kv.setValue(sysOrgDTO.getOrgNm());
            kvList.add(kv);
        }
        return kvList;
    }

    @Override
    public ResponseData submitData() {
        this.fgiBusInfoApiService.searchFgiDataAndSubmitTimerHandler();
        return ResponseData.success();
    }

    @Override
    public ResponseData checkData() {
        this.fgiBusInfoApiService.checkSubmitFgiDataTimerHandler();
        return ResponseData.success();
    }

    @Override
    public ResponseData getData(Date startTm, Date endTm) {
        return this.fgiBusInfoApiService.saveBatchFgiBusInfo(startTm, endTm);
    }

    @Override
    public JSONObject getSubmitDetailByPayNo(String busSourceId, String busSource, String busSourceType) {
        JSONObject result = new JSONObject();
        //查询业务信息
        FgiBusInfoQueryCondition busInfoCondition = new FgiBusInfoQueryCondition();
        busInfoCondition.setBusSourceId(busSourceId);
        busInfoCondition.setBusSource(busSource);
        busInfoCondition.setBusSourceType(busSourceType);
        FgiBusInfoDTO busInfo = this.fgiBusInfoApiService.getFgiBusInfoDTOByBusSourceId(busInfoCondition);
        result.put("busInfo", FgiBusInfoVOTransform.toVO(busInfo));
        if (null != busInfo) {
            //查询用户信息
            FgiUserInfoQueryCondition userInfoCondition = new FgiUserInfoQueryCondition();
            userInfoCondition.setUseridCardHash(busInfo.getUserIdcardHash());
            FgiUserInfoWebDTO userInfo = this.fgiUserInfoApiService.getFgiUserInfoDTOByUseridCardHash(userInfoCondition);
            if (null != userInfo && StringUtils.isNotEmpty(userInfo.getUserPhone())) {
                if (userInfo.getUserPhone().length() == 11) {
                    userInfo.setUserPhone(EncryptUtil.mobileEncrypt(userInfo.getUserPhone()));
                } else {
                    userInfo.setUserPhone(EncryptUtil.encrypt(userInfo.getUserPhone(), 0, 4, "*"));
                }
            }
            result.put("userInfo", userInfo);
        }
        FgiConInfoDTO conInfo = this.fgiConInfoApiService.selectBySourceId(busSourceId, busSource, busSourceType);
        result.put("conInfo", conInfo);
        return result;
    }
}
